package com.xrit.controller;


import com.xrit.exception.CustomException;
import com.xrit.result.Result;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.nio.file.AccessDeniedException;

@RestControllerAdvice
public class BaseExceptionAdvice {

    /**
     * 自定义异常
     * @param ce
     * @return
     */
    @ExceptionHandler(CustomException.class)
    public Result handleCustomException(CustomException ce){
        return new Result(false,ce.getMessage());
    }

    /**
     * 系统异常
     * @param ex
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result handleCustomException(Exception ex){
//        System.out.println("进这里了.....");
        return new Result(false,ex.getMessage());
    }


    /**
     * 密码错误
     * @param he
     * @return
     */
    @ExceptionHandler(BadCredentialsException.class)
    public Result handBadCredentialsException(BadCredentialsException he){
        return handleUserPassword();
    }

    /**
     * 用户名不存在
     * @param he
     * @return
     */
    @ExceptionHandler(InternalAuthenticationServiceException.class)
    public Result handInternalAuthenticationServiceException(InternalAuthenticationServiceException he){
        return handleUserPassword();
    }

    private Result handleUserPassword(){
        return new Result(false, "用户名或密码错误");
    }

    /**
     * 权限不足
     * @param he
     * @return
     */
    @ExceptionHandler(AccessDeniedException.class)
    public Result handAccessDeniedException(AccessDeniedException he){
        return new Result(false, "您暂时没有权限访问，请联系管理员!");
    }
}
